const database = require("../../database/index");
const dayjs = require("dayjs");

// 添加物品
exports.add = (req, res) => {
    const sql = "insert into goods set ?";
    const created = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const picture = req.body.picture ? req.body.picture : '/public/no.png';
    database.query(
        sql,
        {
            ...req.body,
            picture,
            created
        },
        function (err, results) {
            if (err) return res.response(err);
            res.response("添加成功", 0);
        }
    );
};

// 删除物品
exports.delete = (req, res) => {
    const { id } = req.body;
    const sql = "delete from goods where id=?";
    database.query(sql, id, (err) => {
        if (err) return res.response(err);
        res.response("删除成功", 0);
    });
};

// 修改物品
exports.edit = (req, res) => {
    const { id } = req.body;
    let sql = "update goods set ? where id=?";
    const updated = dayjs().format("YYYY-MM-DD HH:mm:ss");
    database.query(sql, [{ ...req.body, updated }, id], (err) => {
        if (err) return res.response(err);
        res.response("修改成功", 0);
    });
};

// 查询物品
exports.list = (req, res) => {
    const { content, page, pageSize, user_id, category_id, type, id, random, status } = req.body;
    let sql = "select goods.*, user.nickname,user.username from goods left join user on goods.user_id=user.id";
    let countSql = "select count(*) as total from goods left join user on goods.user_id=user.id";
    let params = [];
    if (content) {
        sql += sql.includes("where") ? " and (goods.name like?)" : " where (goods.name like?)";
        countSql += countSql.includes("where") ? " and (goods.name like?)" : " where (goods.name like?)";
        params.push(`%${content}%`)
    };
    if (user_id) {
        if (sql.includes("where")) {
            sql += " and goods.user_id =?";
            countSql += " and goods.user_id =?";
        } else {
            sql += " where goods.user_id =?";
            countSql += " where goods.user_id =?";
        }
        params.push(user_id);
    };
    if (category_id) {
        if (sql.includes("where")) {
            sql += " and goods.category_id =?";
            countSql += " and goods.category_id =?";
        } else {
            sql += " where goods.category_id =?";
            countSql += " where goods.category_id =?";
        }
        params.push(category_id);
    };
    if (type) {
        if (sql.includes("where")) {
            sql += " and goods.type =?";
            countSql += " and goods.type =?";
        } else {
            sql += " where goods.type =?";
            countSql += " where goods.type =?";
        }
        params.push(type);
    };
    if (status) {
        if (sql.includes("where")) {
            sql += " and goods.status =?";
            countSql += " and goods.status =?";
        } else {
            sql += " where goods.status =?";
            countSql += " where goods.status =?";
        }
        params.push(status);
    };
    if (random) {
        sql += " ORDER BY goods.created DESC LIMIT ?";
        params.push(random);
    };
    if (id) {
        if (sql.includes("where")) {
            countSql += " and goods.id =?";
        } else {
            countSql += " where goods.id =?";
        }
        params.push(id);
    };
    database.query(countSql, params, function (countErr, countResult) {
        if (countErr) return res.response(countErr);
        const total = countResult[0] ? countResult[0].total : 0;
        if (page && pageSize) {
            const offset = (page - 1) * pageSize;
            sql += " LIMIT ? OFFSET ?";
            params.push(Number(pageSize), offset);
        }
        database.query(sql, params, function (err, result) {
            if (err) return res.response(err);
            return res.send({
                code: 0,
                message: "查询成功",
                total,
                result,
            });
        });
    });
};

